//
//  luoerStatusViewModel.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/20.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit

class luoerStatusViewModel: NSObject {
//   单例
    static let sharedInstance: luoerStatusViewModel = luoerStatusViewModel()
//    私有构造函数
    private override init() {
        super.init()
    }
//    加载微博数据
    func loadStatus(loadStatusCallback: (statuses: [luoerStatus]?, error: NSError?) -> ())
    {
        guard let access_token = luoerUserAccountViewModel.sharedinstance.userAccount?.access_token else
        {
           return
        }
//        UrlString
        let urlString = "2/statuses/home_timeline.json"
        // 参数
        let parameters = [
            "access_token" : access_token
        ]
        //   // 请求
        luoerNetworkTool.sharedInstance.request(RequestMethod.GET, URLString: urlString, parameters: parameters, success: { (_, responseObject) -> Void in
            //            CZPrint(items: "获取微博数据成功了: \(responseObject)")
            if let result = responseObject as? [String: AnyObject] {
                // 返回的数据是字典
                
                // 获取到所有的微博数据
                if let statuses = result["statuses"] as? [[String: AnyObject]] {
                    
                    // 定义空数组数组存放转好的微博模型
                    var statusArray = [luoerStatus]()
                    
                    // 遍历数组,取出每一个微博字典 [String: AnyObject]
                    for dict in statuses {
                        // 字典转模型
                        let statusModel = luoerStatus(dict: dict)
                        
                        // 将模型添加到数组中
                        statusArray.append(statusModel)
                    }
                    loadStatusCallback(statuses: statusArray, error: nil)
                }
            }
            
            }) { (_, error) -> Void in
            loadStatusCallback(statuses: nil, error: error)
        }
    }

}
